package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.IndexImage;
import com.mjfeed.service.IndexImageService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 首页图片
 * @author zhaominglei
 * @date 2015年4月25日
 */
@Controller("adminIndexImageController")
@RequestMapping("/admin/indexImage")
public class IndexImageController extends BaseController {
    @Resource(name="indexImageService")
    private IndexImageService indexImageService;
    
    @RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/indexImage/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<IndexImage> pagination = indexImageService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/indexImage/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/indexImage/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(IndexImage indexImage, HttpServletRequest request, HttpServletResponse response, Model model){
		indexImageService.build(indexImage);
		indexImageService.save(indexImage);
		return "redirect:/admin/indexImage/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("indexImage", indexImageService.getById(IndexImage.class, id));
		return "admin/indexImage/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(IndexImage indexImage, HttpServletRequest request, HttpServletResponse response, Model model){
		IndexImage pIndexImage = indexImageService.getById(IndexImage.class, indexImage.getId());
		if (pIndexImage == null) {
			return ERROR_VIEW;
		}
		indexImageService.build(indexImage);
		indexImage.setSource((indexImage.getSource() == null || indexImage.getSource().equals("") ? pIndexImage.getSource() : indexImage.getSource()));
		indexImageService.update(indexImage);
		return "redirect:/admin/indexImage/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("indexImage", indexImageService.getById(IndexImage.class, id));
		return "admin/indexImage/view";
	}
}